package com.proactive.webapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseCommentReportDAO;
import com.proactive.webapp.dao.CourseReportDAO;
import com.proactive.webapp.dao.DesignCommentReportDAO;
import com.proactive.webapp.dao.DesignReportDAO;
import com.proactive.webapp.dao.ResourceReportDAO;
import com.proactive.webapp.dao.WorksheetCommentReportDAO;
import com.proactive.webapp.dao.WorksheetReportDAO;
import com.proactive.webapp.entity.CourseCommentReport;
import com.proactive.webapp.entity.CourseReport;
import com.proactive.webapp.entity.DesignCommentReport;
import com.proactive.webapp.entity.DesignReport;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.ResourceReport;
import com.proactive.webapp.entity.WorksheetCommentReport;
import com.proactive.webapp.entity.WorksheetReport;

@Controller
public class ReportController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	@RequestMapping(value = "/addReport", method = RequestMethod.POST)
	public ModelAndView addReport(int type, int id, String reason, String remark, 
			HttpSession session, HttpServletRequest request){
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseReportDAO courseReportDAO = (CourseReportDAO) context.getBean("courseReportDAO");
				CourseReport courseReport = new CourseReport();
				courseReport.setCid(id);
				courseReport.setReason(reason);
				courseReport.setRemark(remark);
				courseReport.setAccount(loginAccount);
				courseReportDAO.insert(courseReport);
			}
			else if(type == 2){
				DesignReportDAO designReportDAO = (DesignReportDAO) context.getBean("designReportDAO");
				DesignReport designReport = new DesignReport();
				designReport.setDid(id);
				designReport.setReason(reason);
				designReport.setRemark(remark);
				designReport.setAccount(loginAccount);
				designReportDAO.insert(designReport);
			}
			else if(type == 3){
				WorksheetReportDAO worksheetReportDAO = (WorksheetReportDAO) context.getBean("worksheetReportDAO");
				WorksheetReport worksheetReport = new WorksheetReport();
				worksheetReport.setWid(id);
				worksheetReport.setReason(reason);
				worksheetReport.setRemark(remark);
				worksheetReport.setAccount(loginAccount);
				worksheetReportDAO.insert(worksheetReport);
			}
			else if(type == 4){
				ResourceReportDAO resourceReportDAO = (ResourceReportDAO) context.getBean("resourceReportDAO");
				ResourceReport resourceReport = new ResourceReport();
				resourceReport.setRid(id);
				resourceReport.setReason(reason);
				resourceReport.setRemark(remark);
				resourceReport.setAccount(loginAccount);
				resourceReportDAO.insert(resourceReport);
			}
			else if(type == 5){
				CourseCommentReportDAO courseCommentReportDAO = (CourseCommentReportDAO) context.getBean("courseCommentReportDAO");
				CourseCommentReport courseCommentReport = new CourseCommentReport();
				courseCommentReport.setCommentID(id);
				courseCommentReport.setReason(reason);
				courseCommentReport.setRemark(remark);
				courseCommentReport.setAccount(loginAccount);
				courseCommentReportDAO.insert(courseCommentReport);
			}
			else if(type == 6){
				DesignCommentReportDAO designCommentReportDAO = (DesignCommentReportDAO) context.getBean("designCommentReportDAO");
				DesignCommentReport designCommentReport = new DesignCommentReport();
				designCommentReport.setCommentID(id);
				designCommentReport.setReason(reason);
				designCommentReport.setRemark(remark);
				designCommentReport.setAccount(loginAccount);
				designCommentReportDAO.insert(designCommentReport);
			}
			else if(type == 7){
				WorksheetCommentReportDAO worksheetCommentReportDAO = (WorksheetCommentReportDAO) context.getBean("worksheetCommentReportDAO");
				WorksheetCommentReport worksheetCommentReport = new WorksheetCommentReport();
				worksheetCommentReport.setCommentID(id);
				worksheetCommentReport.setReason(reason);
				worksheetCommentReport.setRemark(remark);
				worksheetCommentReport.setAccount(loginAccount);
				worksheetCommentReportDAO.insert(worksheetCommentReport);
			}
			if(type==5 || type==6 || type==7)
				model.setViewName("redirect:"+request.getHeader("referer")+"#commentArea");
			else
				model.setViewName("redirect:"+request.getHeader("referer"));
			
		}
		else
			model.setViewName("redirect:/login");
		return model;
	}
}
